/**
 * Created by cloud on 17.01.15.
 */
var $ = grp.jQuery;
function addNewPopupWindow(link) {
    var name = link.id.replace(/^add_/, '');
    var container = $(link).closest('div.grp-group.grp-tabular');
    var group = container.attr('id').split('-')[0];
    var lockingInput = $('div.grp-empty-form input[type=hidden]', container).not("[id$=id]");
    addPopupWindow(link.href, name, group, lockingInput);
    return false;
}

function addChangePopupWindow(link) {
    var name = link.id.replace(/^add_/, '');
    var group = $(link).closest('div.grp-group.grp-tabular').attr('id').split('-')[0];
    var lockingInput = $(link).closest('.grp-tr').find('input[type=hidden]').not("[id$=id]");
    addPopupWindow(link.href, name, group, lockingInput);
    return false;
}

function addPopupWindow(href, name, group, lockingInput) {
    lock_parts = lockingInput.attr('name').split('-');
    lock = lock_parts[lock_parts.length - 1];
    lock += "|" + lockingInput.val();
    href = href + "?_inline_popup=1&group=" + group + "&lock=" + lock;
    var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
    win.focus();
}

function dismissChangePopupWindow(win, target, object) {
    win.close();
    var $ = grp.jQuery;
    var group = $("div#" + target + "-group");
    var row;
    var id_field = $("input[id^=id_" + target + "][id$=id][value=" + object.pk + "]", group);
    if (!id_field.length) {
        row = add_inline_form(target);
        $("input[id^=id_" + target + "][id$=id]", row).val(object.pk);
    }
    else
        row = id_field.closest("div.grp-tr");
    $.each(object.fields, function (param, val) {
        $("div." + param + " div", row).html(val);
    });
}

function add_inline_form(name) {
    // get the tr of the first form item, and then count its siblings
    var count = $("#" + name + "-group div.grp-tbody").not(".grp-empty-form").length;
    var emptyRow = $("#" + name + "-group div.grp-tbody.grp-empty-form");
    var copy = emptyRow.clone(true);
    $(copy).find('[id^="id_' + name + '-__prefix__-"]').each(function (index) {
        $(this).attr('id', $(this).attr('id').replace("id_" + name + "-__prefix__-", "id_" + name + "-" + count + "-"));
        $(this).attr('name', $(this).attr('name').replace(name + "-__prefix__-", name + "-" + (count) + "-"));
    });

    $(copy).removeClass("grp-empty-form");
    $(copy).addClass("has_original").addClass("grp-dynamic-form");
    $(copy).attr("id", name + count);

    // change 'remove' to 'delete'
    var removeLink = $("a.grp-remove-handler", copy);
    removeLink.removeClass('grp-remove-handler').addClass('grp-delete-handler');
    removeLink.before($("<input id='id_" + name + "-" + count + "-DELETE' name='" + name + "-" + count + "-DELETE' type='checkbox'>"));
    removeLink.parent().addClass("grp-delete-handler-container");
    removeLink.off('click');
    removeLink.click(deleteHandler);
    emptyRow.before(copy);


    // increase the total number of forms so Django will process them
    // account for the original form we cloned and the new one
    $('input#id_' + name + '-TOTAL_FORMS').val(count + 1);
    $('input#id_' + name + '-INITIAL_FORMS').val(parseInt($('input#id_' + name + '-INITIAL_FORMS').val()) + 1);
    return copy;
}

function deleteHandler() {
    var deleteInput = $(this).prev(),
        form = $(this).parents(".grp-dynamic-form").first();
    // toggle options.predeleteCssClass and toggle checkbox
    if (form.hasClass("has_original")) {
        form.toggleClass('grp-predelete');
        if (deleteInput.prop("checked")) {
            deleteInput.removeAttr("checked");
        } else {
            deleteInput.prop("checked", true);
        }
    }
}